package com.lld.im.service.message.server;

import com.lld.im.common.ResponseVO;
import com.lld.im.common.enums.*;
import com.lld.im.service.friendship.dao.ImFriendShipEntity;
import com.lld.im.service.friendship.model.req.GetRelationReq;
import com.lld.im.service.friendship.service.ImFriendService;
import com.lld.im.service.group.dao.ImGroupEntity;
import com.lld.im.service.group.model.resp.GetRoleInGroupResp;
import com.lld.im.service.group.service.ImGroupMemberService;
import com.lld.im.service.group.service.ImGroupService;
import com.lld.im.service.user.dao.ImUserDataEntity;
import com.lld.im.service.user.service.ImUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 在聊天之前 对聊天的用户进行检验(禁言/禁用 好友关系 群状态)
 */
@Service
public class CheckChartServer {


    @Autowired
    private ImUserService imUserService;

    @Autowired
    private ImFriendService imFriendService;

    @Autowired
    private ImGroupService imGroupService;

    @Autowired
    private ImGroupMemberService imGroupMemberService;


    /**
     * 检查用户状态
     * @return
     */
    public ResponseVO checkUserForbiddenOrProhibitionStatus(Integer appId,String fromId,String toId){
        ResponseVO<ImUserDataEntity> singleUserInfo = imUserService.getSingleUserInfo(fromId, appId);
        //为找到用户
        if(!singleUserInfo.isOk())
            return singleUserInfo;
        if(toId!=null){
            //判断接收方是否存在
            ResponseVO<ImUserDataEntity> toUserInfoResp = imUserService.getSingleUserInfo(toId, appId);
            if(!toUserInfoResp.isOk()){
                return ResponseVO.errorResponse(MessageErrorCodeEnum.TARGET_USER_UNEXACTING);
            }
        }
        ImUserDataEntity userDataEntity = singleUserInfo.getData();
        //用户禁用状态
        if (userDataEntity.getForbiddenFlag()!= UserForbiddenFlagEnum.NORMAL.getCode()) {
            return ResponseVO.errorResponse(MessageErrorCodeEnum.FROMER_IS_FORBIBBEN);
        }
        if (userDataEntity.getSilentFlag()!= UserSilentFlagEnum.NORMAL.getCode()) {
            return ResponseVO.errorResponse(MessageErrorCodeEnum.FROMER_IS_MUTE);
        }
        return ResponseVO.successResponse();
    }

    /**
     * 检查好友关系
     * @param appId
     * @param toUserId
     * @return
     */
    public ResponseVO checkRelation(Integer appId,String fromUser,String toUserId){
        //基于表设计 fromUser和toUser 的状态可能会不一样 需要双向判断
        //TODO 判断自己与好友关系
        ResponseVO relation = getFriendShipEntity(appId, fromUser, toUserId);
        if(!relation.isOk()){
            return relation;
        }
        ImFriendShipEntity  friendShipEntity = (ImFriendShipEntity) relation.getData();
        if(friendShipEntity.getStatus()!= FriendShipStatusEnum.FRIEND_STATUS_NORMAL.getCode()){
            return ResponseVO.errorResponse(MessageErrorCodeEnum.FRIEND_IS_DELETED);
        }
        if(friendShipEntity.getBlack()==FriendShipStatusEnum.BLACK_STATUS_BLACKED.getCode()){
            return ResponseVO.errorResponse(MessageErrorCodeEnum.FRIEND_IS_BLACK);
        }
        //TODO 判断好友与自己的关系
        ResponseVO toRelation = getFriendShipEntity(appId, toUserId, fromUser);
        if(!toRelation.isOk()){
            return toRelation;
        }
        ImFriendShipEntity  toFriendShipEntity = (ImFriendShipEntity) toRelation.getData();
        if(toFriendShipEntity.getStatus()!= FriendShipStatusEnum.FRIEND_STATUS_NORMAL.getCode()){
            return ResponseVO.errorResponse(MessageErrorCodeEnum.YOU_IS_DELETE);
        }
        if(toFriendShipEntity.getBlack()==FriendShipStatusEnum.BLACK_STATUS_BLACKED.getCode()){
            return ResponseVO.errorResponse(MessageErrorCodeEnum.YOU_IS_BLACK);
        }

        return ResponseVO.successResponse();
    }

    private ResponseVO getFriendShipEntity(Integer appId,String fromUser,String toUserId){
        GetRelationReq fromRelReq = new GetRelationReq();
        fromRelReq.setFromId(fromUser);
        fromRelReq.setToId(toUserId);
        fromRelReq.setAppId(appId);
        return imFriendService.getRelation(fromRelReq);
    }

    public ResponseVO checkGroupStatus(Integer appId,String groupId,String fromId){
        //TODO 判断用户状态
        ResponseVO responseVO = checkUserForbiddenOrProhibitionStatus(appId, fromId, null);
        if(!responseVO.isOk())
            return responseVO;
        //TODO 群是否存在
        ResponseVO<ImGroupEntity> groupEntityResponseVO = imGroupService.getGroup(groupId, appId);
        if(!groupEntityResponseVO.isOk()){
            return groupEntityResponseVO;
        }
        //TODO 群状态
        ImGroupEntity groupEntity = groupEntityResponseVO.getData();

        ResponseVO<GetRoleInGroupResp> roleInGroupOne =
                imGroupMemberService.getRoleInGroupOne(groupId, fromId, appId);
        //TODO 用户是否在群
        if(!roleInGroupOne.isOk()){
            return roleInGroupOne;
        }
        GetRoleInGroupResp roleInGroupOneData = roleInGroupOne.getData();
        //群禁言 非群主和管理源不能发消息
        if (groupEntity.getMute()== GroupMuteTypeEnum.MUTE.getCode()&&
                (roleInGroupOneData.getRole()==GroupMemberRoleEnum.MAMAGER.getCode()||
                        roleInGroupOneData.getRole()==GroupMemberRoleEnum.OWNER.getCode())) {
            return ResponseVO.errorResponse(GroupErrorCode.THIS_GROUP_IS_MUTE);
        }
        //TODO 用户在群是否禁言
        //如果为null 默认封禁言状态
        if((roleInGroupOneData.getSpeakDate()==null?0L:roleInGroupOneData.getSpeakDate())
                >System.currentTimeMillis()){
            return ResponseVO.errorResponse(GroupErrorCode.GROUP_MEMBER_IS_SPEAK);
        }

        return ResponseVO.successResponse();
    }

}
